﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using CommDll;

namespace HospitalMedicineTraceability
{
    /// <summary>
    /// ClassName:frmDeptManage
    /// Version:1.0
    /// Date:2012-010-04
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为处理部门管理
    /// </remarks>
    public partial class frmDeptManage : DevComponents.DotNetBar.OfficeForm
    {
        #region 01.自定义属性
        public DataTable m_dt;  // 保存查询出来的数据
        private int m_nRowsIndex0 = -1;
        #endregion
        public frmDeptManage()
        {
            InitializeComponent();
        }

        #region 02.自定义方法

        /// <summary>
        /// 初始化DataGridView字段显示信息
        /// </summary>
        public void _initDgvTitle()
        {
            string strFieldName = "", strFieldSql = "", strFieldWidth0 = "";
            // 1 主表数据表格
            string strDgvName = this.Name + "_01";            
            // 存在时不再处理
            if (AppConfig.m_dictFieldName.ContainsKey(strDgvName))
            {
                AppConfig._initDictField(strDgvName);
                //return;
            }
            strFieldName = "ID-序号,Code-部门编号,Name-部门名称,Remark-备注,CreateName-操作员,CreateTime-操作时间";
            strFieldSql = "ID,Code,Name,Remark,CreateName,CreateTime";
            strFieldWidth0 = "0,80,150,200,80,120";
            AppConfig.m_dictFieldName.Add(strDgvName, strFieldName);
            AppConfig.m_dictFieldSql.Add(strDgvName, strFieldSql);
            AppConfig.m_dictField0.Add(strDgvName, strFieldSql);// 与SqlField同
            AppConfig.m_dictFieldWidth0.Add(strDgvName, strFieldWidth0);
            // 设置值
            strFieldSql = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_Field", strDgvName), AppConfig.m_dictField0[strDgvName]).Trim();
            AppConfig.m_dictField.Add(strDgvName, strFieldSql);
            strFieldWidth0 = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_FieldWidth", strDgvName), AppConfig.m_dictFieldWidth0[strDgvName]).Trim();
            AppConfig.m_dictFieldWidth.Add(strDgvName, strFieldWidth0);
            // 02 右键双击设置显示方式
            this.dgvMain.Tag = strDgvName;
            this.dgvMain.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(DataDeal.dataGridView_MouseDoubleClick);
            // 03 其他事件
            this.dgvMain.ColumnHeaderMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(CommFun._dgvColumnHeaderSort);// 排序
            this.dgvMain.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(CommFun._dgvRowEnter);// 改变行颜色

            // 2 明细数据表格
            strDgvName = this.Name + "_02";
            // 存在时不再处理
            if (AppConfig.m_dictFieldName.ContainsKey(strDgvName))
            {
                AppConfig._initDictField(strDgvName);
                //return;
            }
            strFieldName = "Id-序号,Code-用户编号,Name-用户姓名,Status-状态,Role-角色编号,DataRole-数据权限编号,RoleName-角色,DataRoleName-数据权限,CreateName-操作员,CreateTime-操作时间";
            strFieldSql = "Id,Code,Name,Status,Role,DataRole,RoleName,DataRoleName,CreateName,CreateTime";
            strFieldWidth0 = "0,80,120,0,0,0,200,200,80,120";
            AppConfig.m_dictFieldName.Add(strDgvName, strFieldName);
            AppConfig.m_dictFieldSql.Add(strDgvName, strFieldSql);
            AppConfig.m_dictField0.Add(strDgvName, strFieldSql);// 与SqlField同
            AppConfig.m_dictFieldWidth0.Add(strDgvName, strFieldWidth0);
            // 设置值
            strFieldSql = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_Field", strDgvName), AppConfig.m_dictField0[strDgvName]).Trim();
            AppConfig.m_dictField.Add(strDgvName, strFieldSql);
            strFieldWidth0 = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_FieldWidth", strDgvName), AppConfig.m_dictFieldWidth0[strDgvName]).Trim();
            AppConfig.m_dictFieldWidth.Add(strDgvName, strFieldWidth0);
            // 02 右键双击设置显示方式
            this.dgvDetail.Tag = strDgvName;
            this.dgvDetail.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(DataDeal.dataGridView_MouseDoubleClick);
            // 03 其他事件
            this.dgvDetail.ColumnHeaderMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(CommFun._dgvColumnHeaderSort);// 排序
            this.dgvDetail.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(CommFun._dgvRowEnter);// 改变行颜色
        }

        /// <summary>
        /// 信息查询
        /// </summary>
        private void _query()
        {
            if (m_dt != null)
            {
                m_dt.Dispose();
            }
            // 00 取条件:有参数时按参数来取数据            
            string strSql = string.Format("SELECT {0} FROM TH_System_Dept where 1=1 {{0}} order by Code", AppConfig.m_dictFieldSql[dgvMain.Tag.ToString()]);
            strSql = string.Format(strSql, "");
            m_dt = AppInfo._openSql_DataTable(strSql);
           
            // 03 显示数据
            DataTable dt = null;
            if (m_dt != null)
            {
                dt = m_dt.Copy();
            }
            _showData(dt);
        }

        /// <summary>
        /// 数据显示
        /// </summary>
        /// <param name="dt"></param>
        private void _showData(DataTable dt)
        {
            m_nRowsIndex0 = -1;
            if (dgvDetail.Rows.Count > 0)
            {
                ((DataTable)dgvDetail.DataSource).Rows.Clear();
                gbDetail.Text = string.Format("人员信息【记录数:{0}】", dgvDetail.Rows.Count);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                // 0101 调整字段顺序
                CommFun._dataTableFieldMove(dt, AppConfig.m_dictField[dgvMain.Tag.ToString()]);
                // 0102 显示数据
                CommFun._showDataGridView(dgvMain, dt, AppConfig.m_dictFieldWidth[dgvMain.Tag.ToString()], AppConfig.m_dictFieldName[dgvMain.Tag.ToString()]); 
            }
            else {
                // 清除数据
                DataTable dtTemp = (DataTable)dgvMain.DataSource;
                if (dtTemp != null && dtTemp.Rows.Count > 0)
                {
                    dtTemp.Rows.Clear();
                }      
            }
            gbMain.Text = string.Format("部门记录数:{0}", dgvMain.Rows.Count);
        }

        /// <summary>
        /// 数据显示
        /// </summary>
        /// <param name="dt"></param>
        private void _showDetail()
        {
            // 00 清除数据
            if (dgvDetail.Rows.Count > 0)
            {
                DataTable dtTemp = (DataTable)dgvDetail.DataSource;
                if (dtTemp != null && dtTemp.Rows.Count > 0)
                {
                    dtTemp.Rows.Clear();
                }
            }
            // 01 按编号取数据
            if (m_nRowsIndex0 >= 0)
            {
                string strCode = "";
                strCode = dgvMain.Rows[m_nRowsIndex0].Cells["Code"].Value.ToString();
                try
                {
                    string strSql = "select {0} from (select Id,Code,Name,Status,Role,DataRole,'' as RoleName,'' as DataRoleName,CreateName,CreateTime from TH_User where DeptCode = '{1}')A";
                    strSql = string.Format(strSql, AppConfig.m_dictFieldSql[dgvDetail.Tag.ToString()], strCode);
                    DataTable dt = AppInfo._openSql_DataTable(strSql);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        // 0101 调整字段顺序
                        CommFun._dataTableFieldMove(dt, AppConfig.m_dictField[dgvDetail.Tag.ToString()]);
                        // 0102 显示数据
                        CommFun._showDataGridView(dgvDetail, dt, AppConfig.m_dictFieldWidth[dgvDetail.Tag.ToString()], AppConfig.m_dictFieldName[dgvDetail.Tag.ToString()]);
                    }
                }
                catch (Exception ex)
                {
                    CommFun._messageBoxShow(ex.Message);
                    return;
                }
            } 
            gbDetail.Text = string.Format("用户信息【记录数:{0}】", dgvDetail.Rows.Count);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void _init()
        {
            // 01 dataGridViewX1设为不可编辑
            dgvMain.ReadOnly = true;
            //dgvMain.Anchor = (System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Top));
            // 02 标题
            _initDgvTitle();
            // 03 splitContainer1
            dgvMain.Anchor = (System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Top
                    | System.Windows.Forms.AnchorStyles.Right | System.Windows.Forms.AnchorStyles.Bottom));
            splitContainer1.Tag = this.Name + "_splitContainerHeight";
            splitContainer1.SplitterDistance = FunctionComm._strToInt(AppConfig.m_iniSetup._readValue("Common", splitContainer1.Tag.ToString(), splitContainer1.SplitterDistance.ToString()).Trim(), splitContainer1.SplitterDistance);
            splitContainer1.SplitterMoved += DataDeal.splitContainer_SplitterMoved;
            // 04 查询数据
            _query();
        }
        #endregion

        #region 03.系统事件方法

        private void frm_Load(object sender, EventArgs e)
        {
            _init();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmDeptDetail frm = new frmDeptDetail();
            frm.m_systemDept.m_nOperate = 1;
            frm.ShowDialog();
            if (frm.m_bIsRefresh) {
                _query();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataGridViewRow dr = dgvMain.CurrentRow;
            if (dr != null)
            {
                frmDeptDetail frm = new frmDeptDetail();
                frm.m_systemDept.m_nOperate = 2;
                frm.m_systemDept.m_nId = FunctionComm._strToInt(dr.Cells["Id"].Value.ToString());
                frm.m_systemDept.m_strCode = dr.Cells["Code"].Value.ToString();
                frm.m_systemDept.m_strName = dr.Cells["Name"].Value.ToString();
                frm.ShowDialog();
                if (frm.m_bIsRefresh)
                {
                    dr.Cells["Code"].Value = frm.m_systemDept.m_strCode;
                    dr.Cells["Name"].Value = frm.m_systemDept.m_strName;
                    dr.Cells["CreateName"].Value = frm.m_systemDept.m_strCreateName;
                    dr.Cells["CreateTime"].Value = DateTime.Now;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataGridViewRow dr = dgvMain.CurrentRow;
            if (dr != null)
            {
                DialogResult result = MessageBox.Show("是否确定要删除该部门信息?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    SystemDept model = new SystemDept();
                    model.m_nId = FunctionComm._strToInt(dr.Cells["Id"].Value.ToString());
                    model.m_nOperate = 3;
                    if (model._saveInfo() > 0)
                    {
                        MessageBox.Show("该部门信息删除完成！");
                        dgvMain.Rows.Remove(dr);
                    }
                    else
                    {
                        MessageBox.Show("该部门信息删除失败！");
                    }
                }
            }
        }
        #endregion

        #region 04.GridViewX事件
        private void dataGridViewX1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // 改变行颜色
            CommFun._dgvRowEnter(sender,e);
            // 显示明细
            if (!(e.RowIndex < 0 || e.ColumnIndex < 0))
            {
                if (m_nRowsIndex0 != e.RowIndex)
                {
                    m_nRowsIndex0 = e.RowIndex;
                    _showDetail();
                }
            } 
        }
        #endregion 
    }
}